#!/usr/bin/python

import os,sys, getopt
import re, urllib2

def f5(seq, idfun=None): 
   # order preserving
   if idfun is None:
       def idfun(x): return x
   seen = {}
   result = []
   for item in seq:
       marker = idfun(item)
       # in old Python versions:
       # if seen.has_key(marker)
       # but in new ones:
       if marker in seen: continue
       seen[marker] = 1
       result.append(item)
   return result


def main(argv):
   if len(sys.argv) < 2:
    sys.stderr.write('Script Error: Check Arguments\n')
    sys.stderr.write('-s <site> -o <outputfile>\n')
    sys.exit(1)

   site = ''
   outputfile = ''
   try:
      opts, args = getopt.getopt(argv,"hs:o:",["site=","outputfile="])
   except getopt.GetoptError:
      print  ' -s <site> -o <outputfile>\n'
      sys.exit(2)
   for opt, arg in opts:
      if opt == '-h':
         print ' -s site -o <outputfile>\n'
         sys.exit()
      	
      elif opt in ("-s", "--site"):
         site = arg
      elif opt in ("-o", "--outputfile"):
         outfile = arg
   print 'Site: ', site
   print 'Output File: ', outfile

   site_request = urllib2.urlopen(site)
   html_content = site_request.read().split(' ')
   
   
   chars = ['','!','@','#','%','^','&','*','(',')','!@#','^(','69','123','2012','!@','!!']
   output_list = []
   for word in html_content:
    if len(word) > 3:
     matchObj = re.search(r'a\ href(.*?)',word, re.I)
     if matchObj: print matchObj
     word = filter(lambda c: c.isalpha(), word)
     if word != '':
      output_list.append(word)	 
   

   	
   srt =  sorted(output_list)
   arranged = f5(srt)
   #print f5(srt) //sort list print for debug
   with open(outfile,'w+') as file:
    for item in arranged:
     for char in chars:
      file.write(item+char+'\n')
     #print item
     file.write(item.capitalize()+'\n')
    file.close()  

if __name__ == "__main__":
   main(sys.argv[1:])
